package common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ReceiveFile extends CommonFile {
	private InputStream m_dataInputStream;
//	private ObjectOutputStream m_controlOutputStream;
//	private ObjectInputStream  m_controlInputStream;
//	private File m_File;
//	private Socket m_dataSocket;
//	private Socket m_controlSocket;
	String m_sPathFile;

	public ReceiveFile(File receivefile, Socket dataSocket, Socket controlSocket) {
		this.m_File = receivefile;
		this.m_dataSocket = dataSocket; // Socket de données.
		this.m_controlSocket = controlSocket; // Socket de control ( Message et
												// controle ).
		try {
			this.m_controlOutputStream = new ObjectOutputStream(controlSocket
					.getOutputStream());
			this.m_controlInputStream = new ObjectInputStream(controlSocket
					.getInputStream());
			this.m_dataInputStream = m_dataSocket.getInputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getM_sPathFile() {
		return m_sPathFile;
	}

	public void setM_sPathFile(String mSPathFile) {
		m_sPathFile = mSPathFile;
	}

	// Reçoit un string sur le canal de controle
	String receiveText() {
		String sRet = "";
		try {
			System.out.print("ReceiveFile::receiveText : ");
			sRet = m_controlInputStream.readUTF();
			System.out.println(sRet);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sRet;
	}

	// Envoie un string sur le canal de controle
	void sendText(String sTextToSend) {
		String sRet = "";
		try {
			System.out.println("ReceiveFile::sendText : " + sTextToSend);
			m_controlOutputStream.writeUTF(sTextToSend);
			m_controlOutputStream.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// Envoie un long sur le canal de controle
	long receiveLong() {
		long lRet = 0;
		try {
			lRet = m_controlInputStream.readLong();
			System.out.println("ReceiveFile::receiveLong : " + lRet);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lRet;
	}

	public boolean recoitFichier() throws IOException {
		// Création de l'entrée
		OutputStream outFile = new FileOutputStream(m_sPathFile);
		// Reçoit du client
		BufferedInputStream inBuffer = new BufferedInputStream(
				m_dataInputStream);
		// Envoi vers le fichier
		BufferedOutputStream outBuffer = new BufferedOutputStream(outFile);
		int lu;
		long bitsrecus = 0;
		long bitsarecevoir = 0;
		String sControl = receiveText();
		if (sControl.equalsIgnoreCase("OK")) {
			sendText("READY");
			bitsarecevoir = receiveLong();
			sendText("START");
			while (bitsrecus < bitsarecevoir) {
				lu = inBuffer.read();
				outBuffer.write(lu);
				bitsrecus++;
				//System.out.print("Pourcentage recu : " + (float)bitsrecus/bitsarecevoir * 100 +"%");
			}
		}
			outBuffer.flush();
			outBuffer.close();
			inBuffer.close();
			outFile.flush();
			outFile.close();
	return true;
	}

}
